package org.dromara.kitchen.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.kitchen.domain.coupon.KitchenCoupon;
import org.dromara.kitchen.domain.kitchen.KitchenSetMeal;
import org.dromara.kitchen.service.IBdProvinceService;
import org.dromara.kitchen.service.IKitchenCouponService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 礼品卡、优惠券
 *
 * @author Mr.Zhou
 * @date 2024-12-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/kitchen/coupon")
public class KitchenCouponController extends BaseController {

    private final IKitchenCouponService kitchenCouponService;
    private final IBdProvinceService bdProvinceService;

    /**
     * 查询礼品卡、优惠券列表
     */
    @SaCheckPermission("kitchen:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<KitchenCoupon> list(KitchenCoupon kitchenCoupon, PageQuery pageQuery) {
        QueryWrapper<KitchenCoupon> qw = new QueryWrapper<>();

        TableDataInfo<KitchenCoupon> td = PageUtils.buildDataInfo(kitchenCouponService.page(PageUtils.buildPage(),qw));
        List<KitchenCoupon> list = td.getRows();
        for (KitchenCoupon setMeal : list){
            setMeal.setAreaName(bdProvinceService.getAreaNameByAreaId(setMeal.getProvId(), setMeal.getCityId(), null));
        }
        return td;
    }

    /**
     * 导出礼品卡、优惠券列表
     */
    @SaCheckPermission("kitchen:coupon:export")
    @Log(title = "礼品卡、优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KitchenCoupon kitchenCoupon, HttpServletResponse response) {
        List<KitchenCoupon> list = kitchenCouponService.list();
        ExcelUtil.exportExcel(list, "礼品卡、优惠券", KitchenCoupon.class, response);
    }

    /**
     * 获取礼品卡、优惠券详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("kitchen:coupon:query")
    @GetMapping("/{id}")
    public R<KitchenCoupon> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        KitchenCoupon coupon = kitchenCouponService.getById(id);
        if(coupon!= null){
            if(coupon.getProvId() != null) {
                String[] areas = {coupon.getProvId() + "", coupon.getCityId() + ""};
                coupon.setProvinceArray(areas);
            }
        }
        return R.ok(coupon);
    }

    /**
     * 新增礼品卡、优惠券
     */
    @SaCheckPermission("kitchen:coupon:add")
    @Log(title = "礼品卡、优惠券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody KitchenCoupon kitchenCoupon) {
        if(kitchenCoupon.getProvinceArray()!=null && kitchenCoupon.getProvinceArray().length >= 2){
            kitchenCoupon.setProvId(Integer.parseInt(kitchenCoupon.getProvinceArray()[0]));
            kitchenCoupon.setCityId(Integer.parseInt(kitchenCoupon.getProvinceArray()[1]));
        }
        return toAjax(kitchenCouponService.save(kitchenCoupon));
    }

    /**
     * 修改礼品卡、优惠券
     */
    @SaCheckPermission("kitchen:coupon:edit")
    @Log(title = "礼品卡、优惠券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody KitchenCoupon kitchenCoupon) {
        if(kitchenCoupon.getProvinceArray()!=null && kitchenCoupon.getProvinceArray().length >= 2){
            kitchenCoupon.setProvId(Integer.parseInt(kitchenCoupon.getProvinceArray()[0]));
            kitchenCoupon.setCityId(Integer.parseInt(kitchenCoupon.getProvinceArray()[1]));
        }
        return toAjax(kitchenCouponService.updateById(kitchenCoupon));
    }

    /**
     * 删除礼品卡、优惠券
     *
     * @param ids 主键串
     */
    @SaCheckPermission("kitchen:coupon:remove")
    @Log(title = "礼品卡、优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(kitchenCouponService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
